unit Matrix;

interface

uses Mathematic, GL, GLu;

type
  TMatrixData = array[0..15] of TFloat;
  TMatrixDataPointer = ^TMatrixData;
  TMatrix4f = class
  private
    aData: TMatrixData;
    aDataPointer: TMatrixDataPointer;
    procedure SetData(paData: TMatrixData);
    procedure SetIndexData(I, J:Integer; Value: TFloat);
    function GetIndexData(I, J:Integer): TFloat;
  public
    constructor Create;
    constructor CreateCopy(paMatrix: TMatrix4f);
    destructor Destroy;override;
    procedure MakeCopy(paMatrix: TMatrix4f);
    function Identity: TMatrix4f;
    function Transpose: TMatrix4f;
    function Add(paMatrix: TMatrix4f): TMatrix4f;
    function Subtract(paMatrix: TMatrix4f): TMatrix4f;
    function Multiply(paMatrix: TMatrix4f): TMatrix4f;
    function Inverze3f: TMatrix4f;
    function Inverze2f: TMatrix4f;
    property DataPointer: TMatrixDataPointer read aDataPointer write aDataPointer;
    property Value[I, J: Integer]: TFloat read GetIndexData write SetIndexData;default;
  end;
  TMatrix = TMatrix4f;

implementation

procedure TMatrix4f.SetIndexData(I, J: Integer; Value: TFloat);
begin
    aData[(I * 4) + J] := Value;
end;

function TMatrix4f.GetIndexData(I, J: Integer): TFloat;
begin
    Result := aData[(I * 4) + J];
end;

procedure TMatrix4f.SetData(paData: TMatrixData);
var I, J: Integer;
begin
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            Value[I, J] := Value[I, J];
        end;
    end;
end;

constructor TMatrix4f.Create;
var I, J: Integer;
begin
    inherited Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            Value[I, J] := 0.0;
        end;
    end;
    DataPointer := @aData;
end;

constructor TMatrix4f.CreateCopy(paMatrix: TMatrix4f);
begin
    inherited Create;
    SetData(paMatrix.aData);
    aDataPointer := @aData;
end;

destructor TMatrix4f.Destroy;
begin
    inherited Destroy;
end;

procedure TMatrix4f.MakeCopy(paMatrix: TMatrix4f);
begin
    SetData(paMatrix.aData);
    aDataPointer := @aData;
end;

function TMatrix4f.Identity: TMatrix4f;
var Matrix: TMatrix4f;
    I, J: Integer;
begin
    Matrix := TMatrix4f.Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            if I = J then Matrix.Value[I, J] := 1
            else Matrix.Value[I, J] := 0;
        end;
    end;
    Result := Matrix;
end;

function TMatrix4f.Transpose: TMatrix4f;
var Matrix: TMatrix4f;
    I, J: Integer;
begin
    Matrix := TMatrix4f.Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            Matrix.Value[J, I] := Value[I, J];
        end;
    end;
    Result := Matrix;
end;

function TMatrix4f.Add(paMatrix: TMatrix4f): TMatrix4f;
var Matrix: TMatrix4f;
    I, J: Integer;
begin
    Matrix := TMatrix4f.Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            Matrix.Value[I, J] := Value[I, J] + paMatrix.Value[I, J];
        end;
    end;
    Result := Matrix;
end;

function TMatrix4f.Subtract(paMatrix: TMatrix4f): TMatrix4f;
var Matrix: TMatrix4f;
    I, J: Integer;
begin
    Matrix := TMatrix4f.Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            Matrix.Value[I, J] := Value[I, J] - paMatrix.Value[I, J];
        end;
    end;
    Result := Matrix;
end;

function TMatrix4f.Multiply(paMatrix: TMatrix4f): TMatrix4f;
var Matrix: TMatrix4f;
    I, J, K: Integer;
begin
    Matrix := TMatrix4f.Create;
    for I := 0 to 3 do begin
        for J := 0 to 3 do begin
            for k := 0 to 3 do begin
                Matrix.Value[I, J] := Matrix.Value[I, J] + (Value[I, K] + paMatrix.Value[K, J]);
            end;
        end;
    end;
    Result := Matrix;
end;

function TMatrix4f.Inverze3f: TMatrix4f;
var Matrix: TMatrix4f;
begin
    Matrix := TMatrix4f.Create;
    Result := Matrix;
end;

function TMatrix4f.Inverze2f: TMatrix4f;
var Matrix: TMatrix4f;
begin
    Matrix := TMatrix4f.Create;
    Result := Matrix;
end;

end.
